import type { UploadRequestOptions } from "element-plus";
import { http } from "@/utils/http";
/**
 * 深拷贝
 * @param {*} obj
 * @returns
 */
export const deepClone = obj => {
  return JSON.parse(JSON.stringify(obj));
};

// 获取 sessionStorage 值
export const getSessionItem = item => {
  return sessionStorage.getItem(item);
};
// 移除
export const removeSessionItem = item => {
  sessionStorage.removeItem(item);
};

/***四舍五入指定位数，千位逗号展示 */
export function formatNumPos(data, pos = 2) {
  let numberShow = parseFloat(data || 0);
  return numberShow.toFixed(pos).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
}

/**
 *求多个数组的组合值
[[a,b],[c,d]]=>[[a,c],[a,d],[b,c],[b,d]]
 * @param arrays
 * @returns
 */
export function getCombinations<T = any>(arrays: T[][]): T[][] {
  const result = [];

  function generateCombinations(currentIndex, currentCombination) {
    if (currentIndex === arrays.length) {
      result.push([...currentCombination]);
      return;
    }

    const currentArray = arrays[currentIndex];
    for (let i = 0; i < currentArray.length; i++) {
      currentCombination.push(currentArray[i]);
      generateCombinations(currentIndex + 1, currentCombination);
      currentCombination.pop();
    }
  }

  generateCombinations(0, []);
  return result;
}

export function uploadFun(option: UploadRequestOptions) {
  return http
    .post<MResponse<string>>(option.action, {
      headers: { "content-type": "multipart/form-data" },
      data: { file: option.file }
    })
    .then(res => {
      if (res.code == RESPONSE_CODE.SUCCESS) {
        return res.data;
      }
      return Promise.reject("");
    });
}
